//
//  ZLEmotionPackage.swift
//  表情包数据
//
//  Created by zhanglongtao on 17/1/6.
//  Copyright © 2017年 hanju001. All rights reserved.
//

import UIKit

//表情包模型
class ZLEmotionPackage: NSObject {

    //表情包分组名
    var groupName: String?
    //背景图片
    var bgImageName: String?
    //表情包目录 从目录下加载plist
    var directory: String? {
        didSet{
            
            guard let dicrectory = directory, let path = Bundle.main.path(forResource: "HMEmoticon.bundle", ofType: nil), let bundle = Bundle(path: path),let infoPlist = bundle.path(forResource: "info.plist", ofType: nil, inDirectory: dicrectory), let array = NSArray(contentsOfFile: infoPlist), let models = NSArray.yy_modelArray(with: ZLEmotion.classForCoder(), json: array) as? [ZLEmotion] else {
                
                return
            }
            
            //遍历models设置目录
            for m in models {
                m.directory = directory
            }
            emtions += models
            
        }
    }
    //懒加载表情模型的空数组
    lazy var emtions = [ZLEmotion]()
    
    //表情也数量
    var numberOfPages: Int {
        
        return (emtions.count - 1) / 20 + 1
    }
    //从懒加载的表情包中按照阿哥获取醉倒20个表情模型的数组
    //例如有26个表情
    //例如page = 0 返回0-19个模型
    //page = 1 返回 20- 25个表情
    
    func emotion(page: Int) -> [ZLEmotion] {
        
        let count = 20
        let loca = page * count
        var len = count
        if (loca + len) > emtions.count{
            len = emtions.count - loca
        }
        let range = NSRange(location: loca, length: len)
        let subArray = (emtions as NSArray).subarray(with: range)
        
        return subArray as! [ZLEmotion]
        
    }
    override var description: String {
        
        return yy_modelDescription()
    }
    
}
